import time
from selenium.webdriver.common.by import By
from common.Utils import QE_Driver

#测试问卷考试系统登录页面

class QE_LoginTest:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://8.155.1.153:8081/user/login"
        self.driver = QE_Driver.driver
        self.driver.get(self.url)

    #登录成功的测试用例
    def LoginSuccessTest(self):
        #清空输入框
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#username"))
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#password"))

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("admin")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button > span").click()
        #如果能够找到问卷考试系统首页用户的昵称，说明登录成功，否则登陆失败
        self.driver.find_element(By.XPATH, "/html/body/div[1]/div/div/div/section/div[2]/header[2]/div/div[3]/div/div/div/span/div/span[2]")
        # 添加屏幕截图
        QE_Driver.getScreenshot()

##########################################################################################################################

    #登录异常的测试用例
    def LoginFailTest(self):

        #测试用例一：输入错误的用户名和错误的密码

        #清空输入框
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#username"))
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#password"))

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("error")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR, "#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button > span").click()
        #验证是否登陆失败
        actual = self.driver.find_element(By.CSS_SELECTOR, "#root > div > div.content___2zk1- > div.main___x4OjT > div > form > div.ant-alert.ant-alert-error > div > div").text
        print(f"提示信息: {actual}")
        #添加屏幕截图
        QE_Driver.getScreenshot()
        #断言检测一下是否符合预期
        assert actual == "错误的用户名和密码"

##########################################################################################################################

        # 测试用例二：输入错误的用户名和正确的密码

        #清空输入框
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#username"))
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#password"))

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("error")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button > span").click()
        #验证是否登陆失败
        actual = self.driver.find_element(By.CSS_SELECTOR, "body > #root > div > div.content___2zk1- > div.main___x4OjT > div > form > div.ant-alert.ant-alert-error > div > div").text
        print(f"提示信息: {actual}")
        #添加屏幕截图
        QE_Driver.getScreenshot()
        #断言检测一下是否符合预期
        assert actual == "错误的用户名和密码"

        ##########################################################################################################################

        # 测试用例三：输入正确的用户名和错误的密码

        #清空输入框
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#username"))
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#password"))

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("admin")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR, "#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button > span").click()
        #验证是否登陆失败
        actual = self.driver.find_element(By.CSS_SELECTOR, "#root > div > div.content___2zk1- > div.main___x4OjT > div > form > div.ant-alert.ant-alert-error > div > div").text
        print(f"提示信息: {actual}")
        #添加屏幕截图
        QE_Driver.getScreenshot()
        #断言检测一下是否符合预期
        assert actual == "错误的用户名和密码"

        ##########################################################################################################################

        # 测试用例四：不输入用户名但输入密码

        #清空输入框
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#username"))
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#password"))

        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button > span").click()
        #验证是否登陆失败
        actual = self.driver.find_element(By.CSS_SELECTOR, "#username_help > div").text
        print(f"提示信息: {actual}")
        #添加屏幕截图
        QE_Driver.getScreenshot()
        #断言检测一下是否符合预期
        assert actual == "用户名是必填项！"

        ##########################################################################################################################

        # 测试用例五：输入用户名但不输入密码

        #清空输入框
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#username"))
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#password"))

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("admin")
        self.driver.find_element(By.CSS_SELECTOR, "#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button > span").click()
        #验证是否登陆失败
        actual = self.driver.find_element(By.CSS_SELECTOR, "#password_help > div").text
        print(f"提示信息: {actual}")
        #添加屏幕截图
        QE_Driver.getScreenshot()
        #断言检测一下是否符合预期
        assert actual == "密码是必填项！"



#测试
# login = QE_LoginTest()
# login.LoginFailTest()
# login.LoginSuccessTest()

# 指定浏览器的退出
# QE_Driver.driver.quit()
